$('.menu-op li').click(function () {
    $(this).addClass('active')
    $(this).siblings().removeClass('active')
    $('.content>div').eq($(this).index()).addClass('show')
    $('.content>div').eq($(this).index()).siblings().removeClass('show')
})
var elem = `
            <div class="ipt-de">
                <input type="text">
                <input type="text">
                <span>添加</span>
                <span>删除</span>
            </div>
            `
$('#iptbox').on('click', $('.ipt-de span').eq(21), function (e) {
    if (e.target.innerText == '添加') {
        $('#iptbox').append(elem)
        arr.length = 0
    }
})

let arr = [];

$('#iptbox').on('click', $('.ipt-de span').eq(2), function (e) {
    if (e.target.innerText == '删除') {
        arr.length = 0
        $('#iptbox div').eq($(this).index()).remove()
    }
})

$('#confirm').click(function () {
    arr.length = 0
    for (let i = 0; i < $('#iptbox input').length; i++) {
        arr.push({ domain: $($('#iptbox input')[i]).val(), server: $($('#iptbox input')[i + 1]).val() })
        i++
    }
    console.log(arr);
    /*
    // 清空输入框中的值
    if (arr.length !== 0) {
        for (let i = 0; i < $('#iptbox input').length; i++) {
            $($('#iptbox input')[i]).val('')
        }
        // 只保留第一个、其他全部删除
        $("#iptbox>div").not(":eq(0)").remove();
    }
    */
    chrome.apipe.PrintString('cmd_modify_vps_map\r\n'+strJson, function(ix,s){
        alert(s)
    })
})
function final(data) {
    let server = [];
    let rules = data.rules;
    data.server.map(item => {
        server.push({
            tag: item.tag,
            ip: item.ip,
            port: item.port,
            domain: []
        })
    })
    for (let i = 0; i < server.length; i++) {
        for (let j = 0; j < rules.length; j++) {
            if (server[i].tag == rules[j].tag) {
                server[i].domain = [...server[i].domain, ...rules[j].domain]
            }
        }
    }
    console.log(server);

    // 页面添加对应的输入框
    for (let i = 0; i < server.length-1; i++) {
        $('#iptbox').append(elem)
    }

    //  把内容填充到页面
        let elemArr = []
        for (let j = 0; j < $($('#iptbox input')).length; j++) {
            if(j%2 !==0){
                elemArr.push({
                    domain:$($('#iptbox input'))[j-1],
                    ip:$($('#iptbox input'))[j]
                })
            }
        }
        for (let i = 0; i < server.length; i++) {
            $(elemArr[i].domain).val(server[i].domain.join(','))
            $(elemArr[i].ip).val(server[i].tag)
        }
}

chrome.apipe.PrintString('cmd_get_v2_config', function(ix,s){
    console.log('123')
    console.log('回调收到的返回值:[' + ix  + ',' + s + ']')
    final(JSON.parse(s))
    //callback_count += 1
    //$('.retval').text('回调收到的返回值:[' + ix  + ',' + s + '],回调次数(' + callback_count + ')')
    })

